1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.applet;
27
28 import java.io.BufferedInputStream;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.InvocationTargetException;
35 import java.net.URL;
36 import java.net.MalformedURLException;
37 import java.util.Enumeration;
38 import java.util.Properties;
39 import java.util.Vector;
40 import sun.net.www.ParseUtil;
41
42
43
44
45 public class Main {
46
47
48
49 static File theUserPropertiesFile;
50
51
52
53
54 static final String [][] avDefaultUserProps = {
55
56
57
58
59 {"http.proxyHost", ""},
60 {"http.proxyPort", "80"},
61 {"package.restrict.access.sun", "true"}
62 };
63
64 static {
65 File userHome = new File(System.getProperty("user.home"));
66
67 userHome.canWrite();
68
69 theUserPropertiesFile = new File(userHome, ".appletviewer");
70 }
71
72
73 private static AppletMessageHandler amh = new AppletMessageHandler("appletviewer");
74
75
76
77
78 private boolean debugFlag = false;
79 private boolean helpFlag = false;
80 private String encoding = null;
81 private boolean noSecurityFlag = false;
82 private static boolean cmdLineTestFlag = false;
83
84
85
86
87 private static Vector urlList = new Vector(1);
88
89
90
91 public static final String theVersion = System.getProperty("java.version");
92
93
94
95
96 public static void main(String [] args) {
97 Main m = new Main();
98 int ret = m.run(args);
99
100
101
102
103 if ((ret != 0) || (cmdLineTestFlag))
104 System.exit(ret);
105 }
106
107 private int run(String [] args) {
108
109 try {
110 if (args.length == 0) {
111 usage();
112 return 0;
113 }
114 for (int i = 0; i < args.length; ) {
115 int j = decodeArg(args, i);
116 if (j == 0) {
117 throw new ParseException(lookup("main.err.unrecognizedarg",
118 args[i]));
119 }
120 i += j;
121 }
122 } catch (ParseException e) {
123 System.err.println(e.getMessage());
124 return 1;
125 }
126
127
128 if (helpFlag) {
129 usage();
130 return 0;
131 }
132
133 if (urlList.size() == 0) {
134 System.err.println(lookup("main.err.inputfile"));
135 return 1;
136 }
137
138 if (debugFlag) {
139
140
141
142
143 return invokeDebugger(args);
144 }
145
146
147 if (!noSecurityFlag && (System.getSecurityManager() == null))
148 init();
149
150
151 for (int i = 0; i < urlList.size(); i++) {
152 try {
153
154
155
156 AppletViewer.parse((URL) urlList.elementAt(i), encoding);
157 } catch (IOException e) {
158 System.err.println(lookup("main.err.io", e.getMessage()));
159 return 1;
160 }
161 }
162 return 0;
163 }
164
165 private static void usage() {
166 System.out.println(lookup("usage"));
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181 private int decodeArg(String [] args, int i) throws ParseException {
182 String arg = args[i];
183 int argc = args.length;
184
185 if ("-help".equalsIgnoreCase(arg) || "-?".equals(arg)) {
186 helpFlag = true;
187 return 1;
188 } else if ("-encoding".equals(arg) && (i < argc - 1)) {
189 if (encoding != null)
190 throw new ParseException(lookup("main.err.dupoption", arg));
191 encoding = args[++i];
192 return 2;
193 } else if ("-debug".equals(arg)) {
194 debugFlag = true;
195 return 1;
196 } else if ("-Xnosecurity".equals(arg)) {
197
198
199
200
201 System.err.println();
202 System.err.println(lookup("main.warn.nosecmgr"));
203 System.err.println();
204
205 noSecurityFlag = true;
206 return 1;
207 } else if ("-XcmdLineTest".equals(arg)) {
208
209
210
211 cmdLineTestFlag = true;
212 return 1;
213 } else if (arg.startsWith("-")) {
214 throw new ParseException(lookup("main.err.unsupportedopt", arg));
215 } else {
216
217 URL url = parseURL(arg);
218 if (url != null) {
219 urlList.addElement(url);
220 return 1;
221 }
222 }
223 return 0;
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237 private URL parseURL(String url) throws ParseException {
238 URL u = null;
239
240 String prefix = "file:";
241
242 try {
243 if (url.indexOf(':') <= 1)
244 {
245
246 u = ParseUtil.fileToEncodedURL(new File(url));
247 } else if (url.startsWith(prefix) &&
248 url.length() != prefix.length() &&
249 !(new File(url.substring(prefix.length())).isAbsolute()))
250 {
251
252
253
254 String path = ParseUtil.fileToEncodedURL(new File(System.getProperty("user.dir"))).getPath() +
255 url.substring(prefix.length());
256 u = new URL("file", "", path);
257 } else {
258
259 u = new URL(url);
260 }
261 } catch (MalformedURLException e) {
262 throw new ParseException(lookup("main.err.badurl",
263 url, e.getMessage()));
264 }
265
266 return u;
267 }
268
269
270
271
272
273
274
275
276 private int invokeDebugger(String [] args) {
277
278 String [] newArgs = new String[args.length + 1];
279 int current = 0;
280
281
282
283
284
285
286
287 String phonyDir = System.getProperty("java.home") +
288 File.separator + "phony";
289 newArgs[current++] = "-Djava.class.path=" + phonyDir;
290
291
292 newArgs[current++] = "sun.applet.Main";
293
294
295
296 for (int i = 0; i < args.length; i++) {
297 if (!("-debug".equals(args[i]))) {
298 newArgs[current++] = args[i];
299 }
300 }
301
302
303
304
305
306
307
308
309 try {
310 Class c = Class.forName("com.sun.tools.example.debug.tty.TTY", true,
311 ClassLoader.getSystemClassLoader());
312 Method m = c.getDeclaredMethod("main",
313 new Class[] { String[].class });
314 m.invoke(null, new Object[] { newArgs });
315 } catch (ClassNotFoundException cnfe) {
316 System.err.println(lookup("main.debug.cantfinddebug"));
317 return 1;
318 } catch (NoSuchMethodException nsme) {
319 System.err.println(lookup("main.debug.cantfindmain"));
320 return 1;
321 } catch (InvocationTargetException ite) {
322 System.err.println(lookup("main.debug.exceptionindebug"));
323 return 1;
324 } catch (IllegalAccessException iae) {
325 System.err.println(lookup("main.debug.cantaccess"));
326 return 1;
327 }
328 return 0;
329 }
330
331 private void init() {
332
333 Properties avProps = getAVProps();
334
335
336
337
338
339
340 avProps.put("browser", "sun.applet.AppletViewer");
341 avProps.put("browser.version", "1.06");
342 avProps.put("browser.vendor", "Oracle Corporation");
343 avProps.put("http.agent", "Java(tm) 2 SDK, Standard Edition v" + theVersion);
344
345
346
347 avProps.put("package.restrict.definition.java", "true");
348 avProps.put("package.restrict.definition.sun", "true");
349
350
351
352
353
354
355
356 avProps.put("java.version.applet", "true");
357 avProps.put("java.vendor.applet", "true");
358 avProps.put("java.vendor.url.applet", "true");
359 avProps.put("java.class.version.applet", "true");
360 avProps.put("os.name.applet", "true");
361 avProps.put("os.version.applet", "true");
362 avProps.put("os.arch.applet", "true");
363 avProps.put("file.separator.applet", "true");
364 avProps.put("path.separator.applet", "true");
365 avProps.put("line.separator.applet", "true");
366
367
368
369 Properties sysProps = System.getProperties();
370 for (Enumeration e = sysProps.propertyNames(); e.hasMoreElements(); ) {
371 String key = (String) e.nextElement();
372 String val = (String) sysProps.getProperty(key);
373 String oldVal;
374 if ((oldVal = (String) avProps.setProperty(key, val)) != null)
375 System.err.println(lookup("main.warn.prop.overwrite", key,
376 oldVal, val));
377 }
378
379
380 System.setProperties(avProps);
381
382
383 if (!noSecurityFlag) {
384 System.setSecurityManager(new AppletSecurity());
385 } else {
386 System.err.println(lookup("main.nosecmgr"));
387 }
388
389
390 }
391
392
393
394
395
396
397
398
399
400
401
402 private Properties getAVProps() {
403 Properties avProps = new Properties();
404
405 File dotAV = theUserPropertiesFile;
406 if (dotAV.exists()) {
407
408 if (dotAV.canRead()) {
409
410 avProps = getAVProps(dotAV);
411 } else {
412
413 System.err.println(lookup("main.warn.cantreadprops",
414 dotAV.toString()));
415 avProps = setDefaultAVProps();
416 }
417 } else {
418
419
420
421 File userHome = new File(System.getProperty("user.home"));
422 File dotHJ = new File(userHome, ".hotjava");
423 dotHJ = new File(dotHJ, "properties");
424 if (dotHJ.exists()) {
425
426 avProps = getAVProps(dotHJ);
427 } else {
428
429 System.err.println(lookup("main.warn.cantreadprops",
430 dotHJ.toString()));
431 avProps = setDefaultAVProps();
432 }
433
434
435 try {
436 FileOutputStream out = new FileOutputStream(dotAV);
437 avProps.store(out, lookup("main.prop.store"));
438 out.close();
439 } catch (IOException e) {
440 System.err.println(lookup("main.err.prop.cantsave",
441 dotAV.toString()));
442 }
443 }
444 return avProps;
445 }
446
447
448
449
450
451
452
453 private Properties setDefaultAVProps() {
454 Properties avProps = new Properties();
455 for (int i = 0; i < avDefaultUserProps.length; i++) {
456 avProps.setProperty(avDefaultUserProps[i][0],
457 avDefaultUserProps[i][1]);
458 }
459 return avProps;
460 }
461
462
463
464
465
466
467
468
469
470 private Properties getAVProps(File inFile) {
471 Properties avProps = new Properties();
472
473
474 Properties tmpProps = new Properties();
475 try {
476 FileInputStream in = new FileInputStream(inFile);
477 tmpProps.load(new BufferedInputStream(in));
478 in.close();
479 } catch (IOException e) {
480 System.err.println(lookup("main.err.prop.cantread",
481 inFile.toString()));
482 }
483
484
485 for (int i = 0; i < avDefaultUserProps.length; i++) {
486 String value = tmpProps.getProperty(avDefaultUserProps[i][0]);
487 if (value != null) {
488
489 avProps.setProperty(avDefaultUserProps[i][0], value);
490 } else {
491
492 avProps.setProperty(avDefaultUserProps[i][0],
493 avDefaultUserProps[i][1]);
494 }
495 }
496 return avProps;
497 }
498
499
500
501
502
503 private static String lookup(String key) {
504 return amh.getMessage(key);
505 }
506
507 private static String lookup(String key, String arg0) {
508 return amh.getMessage(key, arg0);
509 }
510
511 private static String lookup(String key, String arg0, String arg1) {
512 return amh.getMessage(key, arg0, arg1);
513 }
514
515 private static String lookup(String key, String arg0, String arg1,
516 String arg2) {
517 return amh.getMessage(key, arg0, arg1, arg2);
518 }
519
520 class ParseException extends RuntimeException
521 {
522 public ParseException(String msg) {
523 super(msg);
524 }
525
526 public ParseException(Throwable t) {
527 super(t.getMessage());
528 this.t = t;
529 }
530
531 Throwable t = null;
532 }
533 }